//
//  MainViewController.m
//  DIYCityKorea
//
//  Created by 훈준 박 on 10. 4. 27..
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import	"WriteDetailController.h"
#import "DIYCityKoreaAppDelegate.h"

@implementation MainViewController
@synthesize cameraBtn;

#pragma mark Image Picker
-(IBAction)cameraBtnTouched:(id)sender {
	BOOL cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
	BOOL photosAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
	
	if (!cameraAvailable && !photosAvailable) {
		return; /* Error handling */
	}
	
	UIImagePickerController* picker = [[UIImagePickerController alloc] init];
	if (cameraAvailable) {
		picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	} else {
		picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	}
	picker.delegate = self;
	picker.allowsEditing = YES;
	[self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	NSData *imageData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerEditedImage"], 1.0);
	
	DIYCityKoreaAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	delegate.takenPhoto = imageData;
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];

	NSString *filename = [documentsDirectory stringByAppendingPathComponent:DIYCITY_TEMP_IMAGE_FILE_NAME];
	delegate.imageFilePath = filename;
	[imageData writeToFile:filename atomically:NO];

	[picker dismissModalViewControllerAnimated:NO];
	[picker release];

	WriteDetailController *writeDetailController = [[WriteDetailController alloc] initWithNibName:@"WriteDetailController" bundle:nil];
	[self.navigationController pushViewController:writeDetailController animated:YES];
	[self.navigationController setTitle:@"제보하기"];
	[writeDetailController release];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[picker dismissModalViewControllerAnimated:YES];
	[picker release];
}


- (void)viewDidLoad {	
	[self.navigationController.navigationBar setHidden:YES];
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];	
}

- (void)viewDidUnload {
	self.cameraBtn = nil;	
}


- (void)dealloc {
	[cameraBtn release];
    [super dealloc];
}


@end
